package edu.drexel.jrexel2d.objects2D;

import java.io.Serializable;

import org.newdawn.slick.opengl.Texture;

import edu.drexel.jrexel2d.animation.Sprite;
import edu.drexel.jrexel2d.animation.SpriteSheet;
import edu.drexel.jrexel2d.physics.PhysicsEngine;

public class Object2D implements Serializable {
	private transient Sprite sprite;
	protected transient SpriteSheet current;
	protected int curFrame;
	private int depth; // Denotes the object's depth, used for initial collision
						// detection
	private Point2D center;
	protected int id;
	private int direction;
	private int height;
	private int width;
	private boolean movable = false;
	private boolean pickable = false;
	private boolean currentlyPicked = false;
	protected transient PhysicsEngine refPhysicsEngine;

	public Object2D(Sprite s, int i) {
		sprite = s;
		depth = 10; // TODO: work out what a good depth will be
		id = i;
		curFrame = 0;
		current = sprite.getSheet("idle");
		this.width = current.getWidth();
		this.height = current.getHeight();
	}

	public Sprite getSprite() {
		return sprite;
	}

	public Texture getTex() {
		if (current == null) {
			current = sprite.getSheet("idle");
			this.width = current.getWidth();
			this.height = current.getHeight();
		}
		return current.getTex();
	}

	public Boolean getBounce() {
		if (current == null)
			return false;
		return current.getBounce();
	}

	public void setLocation(int x, int y) {
		center = new Point2D(x, y);
	}
	
	public void setLocation(float x, float y) {
		center = new Point2D(x, y);
	}

	public void setLocation(Point2D p) {
		center = p;
	}

	public Point2D getLocation() {
		return center;
	}

	public Point2D getBase() { // returns the bottom center location of the
								// object
		return new Point2D(center, 0, getHeight() / 2);
	}

	public int getWidth() {
		/*
		 * if(current == null) return 0 ; return current.getWidth() ;
		 */
		return this.width;
	}

	public int getHeight() {
		/*
		 * if(current == null) return 0 ; return current.getHeight() ;
		 */
		return this.height;
	}

	public int numFrames() {
		if (current == null)
			return 0;
		return current.numFrames();
	}

	public int getFrame() {
		return curFrame;
	}

	public void setSheet(String name) {
		if (!current.getName().equals(name)) {
			SpriteSheet next = sprite.getSheet(name);
			if(next != null) {
				current = next ;
				this.width = current.getWidth();
				this.height = current.getHeight();
				
				curFrame = 0;
			}
		}
	}

	public int getId() {
		return id;
	}

	public int getDepth() {
		return depth;
	}

	public void animate() {
		nextFrame();
	}

	private void nextFrame() {
		if (current == null)
			return;

		if ((curFrame == current.numFrames() - 1) && current.getBounce()) {
			direction = -1;
		}

		if (curFrame == 0) {
			direction = 1;
		}
		curFrame = (curFrame + direction) % current.numFrames();
	}

	public void shift(float x, float y) {
		center.shiftF(x, y);
	}
	
	public void setMovable(boolean movableFlag){
		movable = movableFlag;
	}
	
	public boolean isMovable(){
		return movable;
	}
	
	public void setPickable(boolean pickableFlag){
		pickable = pickableFlag;
	}
	
	public boolean isPickable(){
		return pickable;
	}
	
	public void setCurrentlyPicked(boolean currentlyPickedFlag){
		currentlyPicked = currentlyPickedFlag;
	}
	
	public boolean isCurrentlyPicked(){
		return currentlyPicked;
	}
	
	public boolean isThrown(){
		return refPhysicsEngine.getPhysicsObjectById(this.id).isThrown();
	}
	
	public boolean isRolled(){
		return refPhysicsEngine.getPhysicsObjectById(this.id).isRolled();
	}
	
	public void setPhysicsEngine(PhysicsEngine inPhysicsEngine) {
		refPhysicsEngine = inPhysicsEngine;
	}

}
